package newcoder.lx_map的遍历和排序;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * @Author Mr.wang
 * @Description
 * @Date 2020-04-13
 */
public class Main {
    public static void main(String[] args) {
        // 第一种
        Map<String, String> map = new HashMap<>(4);
        map.put("4", "d");
        map.put("1", "a");
        map.put("2", "b");
        map.put("3", "c");

        for (String o : map.keySet()) {
            System.out.println("key:" + o + "-----value:" + map.get(o));
        }
        // 遍历所有的value
        for (String o : map.values()) {
            System.out.println("value:" + o);
        }

        // 获取所有的键值对
        for (Map.Entry<String, String> entry : map.entrySet()) {
            System.out.println("键值对：" + entry);
            System.out.println("key:" + entry.getKey());
            System.out.println("value:" + entry.getValue());
        }
        // 迭代
        Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, String> next = iterator.next();
            String key = next.getKey();
            String value = next.getValue();
        }
        // Java 8 新特性
        map.forEach((k, v) -> {
            System.out.println("key:" + k + "---------value:" + v);
        });
    }
}
